package me.zhengjie.logging.rest

import io.swagger.v3.oas.annotations.media.Schema
import jakarta.servlet.http.HttpServletResponse
import me.zhengjie.common.utils.*
import me.zhengjie.logging.annotation.Log
import me.zhengjie.logging.service.SysLogService
import me.zhengjie.logging.service.dto.SysLogQueryCriteria
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*
import java.io.IOException

/**
 * @author Kuki Wu
 * @date 2024-11-24
 */
@RestController
@RequestMapping("/api/logs")
@Schema(description = "监控：日志管理")
class SysLogController(private val sysLogService: SysLogService) {
//    @Log("导出数据")
//    @Schema(description = "导出数据")
//    @GetMapping(value = ["/download"])
//    @PreAuthorize("@el.check()")
//    @Throws(
//        IOException::class
//    )
//    fun download(response: HttpServletResponse?, criteria: SysLogQueryCriteria?) {
//        sysLogService.download(sysLogService.queryAll(criteria), response)
//    }

    @Log("导出数据")
    @Schema(description = "导出数据")
    @GetMapping(value = ["/download"])
    @PreAuthorize("@el.check()")
    @Throws(IOException::class)
    fun  exportLog(response:HttpServletResponse, criteria: SysLogQueryCriteria) {
        criteria.logType = "INFO"
        //return ResponseEntity(sysLogService.queryAll(criteria, pageable), HttpStatus.OK)
        sysLogService.download(sysLogService.queryAll(criteria), response)
    }

    @Log("导出错误数据")
    @Schema(description = "导出错误数据")
    @GetMapping(value = ["/error/download"])
    @PreAuthorize("@el.check()")
    @Throws(
        IOException::class
    )
    fun exportErrorLog(response: HttpServletResponse?, criteria: SysLogQueryCriteria) {
        criteria.logType ="ERROR"
        sysLogService.download(sysLogService.queryAll(criteria), response)
    }

    @GetMapping
    @Schema(description ="日志查询")
    @PreAuthorize("@el.check()")
    fun queryLog(criteria: SysLogQueryCriteria, pageable: Pageable?): ResponseEntity<Any?>? {
        criteria.logType = "INFO"
        return ResponseEntity(sysLogService.queryAll(criteria, pageable), HttpStatus.OK)
    }

    @GetMapping(value = ["/user"])
    @Schema(description = "用户日志查询")
    fun queryUserLogs(criteria: SysLogQueryCriteria, pageable: Pageable?): ResponseEntity<*> {
        criteria.logType = "INFO"
        //criteria.blurry = SecurityUtils.username
        criteria.username = SecurityUtils.username
        return ResponseEntity(sysLogService.queryAllByUser(criteria, pageable), HttpStatus.OK)
    }

    @GetMapping(value = ["/error"])
    @Schema(description = "错误日志")
    @PreAuthorize("@el.check()")
    fun queryErrorLog(criteria: SysLogQueryCriteria, pageable: Pageable?): ResponseEntity<*> {
        criteria.logType = "ERROR"
        return ResponseEntity(sysLogService.queryAll(criteria, pageable), HttpStatus.OK)
    }


    @GetMapping(value = ["/error/{id}"])
    @Schema(description = "日志异常详情查询")
    @PreAuthorize("@el.check()")
    fun queryErrorLogDetail(@PathVariable id: Long?): ResponseEntity<Any?>? {
        return ResponseEntity(sysLogService.findByErrDetail(id), HttpStatus.OK)
    }

    @DeleteMapping(value = ["/del/error"])
    @Log("删除所有ERROR日志")
    @Schema(description = "删除所有ERROR日志")
    @PreAuthorize("@el.check()")
    fun delAllErrorLog(): ResponseEntity<Any?>? {
        sysLogService.delAllByError()
        return ResponseEntity(HttpStatus.OK)
    }

    @Log("删除所有INFO日志")
    @Schema(description = "删除所有INFO日志")
    @DeleteMapping(value = ["/del/info"])
    @PreAuthorize("@el.check()")
    fun delAllInfoLog(): ResponseEntity<Any?>? {
        sysLogService.delAllByInfo()
        return ResponseEntity(HttpStatus.OK)
    }
}